//
// Created by denglibin on 2019/6/24 0024.
//
/******************队列的链式存储实现**********************/
#include <stdio.h>
#include <stdlib.h>
/**
 * 链表节点
 */
typedef struct queue_node
{
    int data;
    struct queue_node *next;
} QueueNode;

/**
 * 链表队列
 */
typedef struct linked_queue
{
    QueueNode *head;
    QueueNode *tail;
    int size;
} LinkedQueue;

/**
 * 创建节点
 * @param data
 * @return
 */
static QueueNode *createNode(int data)
{
    QueueNode *node = (QueueNode *)malloc(sizeof(QueueNode));
    node->data = data;
    node->next = NULL;
    return node;
}

/**
 * 创建队列
 * @return
 */
static LinkedQueue *createLinkedQueue()
{
    LinkedQueue *queue = (LinkedQueue *)malloc(sizeof(LinkedQueue));
    queue->size = 0;
    queue->head = NULL;
    queue->tail = NULL;
    return queue;
}

/**
 * 入队
 * @param queue
 * @param data
 */
static void en_queue(LinkedQueue *queue, int data)
{
    QueueNode *node = createNode(data);
    if (queue->size == 0)
    {
        queue->head = queue->tail = node;
    }
    else
    {
        queue->tail->next = node;
        queue->tail = node;
    }
    queue->size++;
}

/**
 * 出队
 * @param queue
 */
static int de_queue(LinkedQueue *queue)
{
    if (queue->size == 0)
    {
        printf("queue is empty\n");
        return -1;
    }
    QueueNode *head = queue->head;
    queue->head = head->next;
    int data = head->data;
    free(head);
    queue->size--;
    return data;
}

/**
 * 释放空间
 * @param queue
 */
static void freeLinkedQueue(LinkedQueue *queue)
{
    QueueNode *temp = queue->head;
    QueueNode *next;
    while (temp)
    {
        next = temp->next;
        free(temp);
        temp = next;
    }
    free(queue);
}

int main23()
{
    LinkedQueue *queue = createLinkedQueue();
    for (int i = 0; i < 10; i++)
    {
        en_queue(queue, i);
    }
    for (int i = 0; i < 12; i++)
    {
        printf("%d\n", de_queue(queue));
    }
    free(queue);

    return 0;
}
